import {ref,computed}  from "vue"
import {defineStore} from "pinia"
import { type IUserInfo , type Token} from "@/interFace/user"
import router from '@/router/index'
// import router from '@/router/index'

export const userInfoStore = defineStore('user',{
  state:():{
    userInfo: Partial<IUserInfo>,
    token:Token,
    isLogin:boolean
  }=>{
    return {
      userInfo:{},
      token:"",
      isLogin:false
    }
  },
actions:{
  setInfo<T>(data:IUserInfo){
    this.userInfo = data
  },
  setLogin<T>(token:Token){
    this.token = token
    this.isLogin = true
  },

  outLogin<T>(){
    this.token = ""
    this.isLogin = false
    this.userInfo = {}
    router.replace({ name: 'Home' }) // TypeError: router is undefined
    console.log('退出登录stute');
  }
},
// persist:true, //开启持久化 不配置等于存全部的 state

// 精细化的方式持久化  
  persist: [
    {
      paths: ['token', 'isLogin','userInfo'],
      storage: localStorage,
    },
    // {
    //   paths: ['userInfo'],
    //   storage: localStorage,
    // },
  ],

})